<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Article;

class ArticleController extends Controller
{
	public function actionIndex()
	{
		$article=Article::find();
		$pagination = new \yii\data\Pagination(['totalCount'=>$article->count(),'pageSize'=>10]);
		$data=$article->offset($pagination->offset)->limit($pagination->limit)->all();

		return $this->render('index',['data'=>$data,'pagination'=>$pagination]);
	}

	public function actionAdd()
	{
		$model=new Article();

		if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['index']);
		}

		return $this->render('add',['model'=>$model]);
	}

	public function actionEdit($id)
	{
		$id=(int)$id;
		if ($id>0 && ($model=Article::findOne($id))) {
			if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post()) && $model->save()) {
				return $this->redirect(['index']);
			}
			return $this->render('edit',['model'=>$model]);
		}
		return $this->redirect(['index']);
	}

	public function actionDelete($id)
	{
		$id=(int)$id;
		if ($id>0) {
			Article::findOne($id)->delete();
		}
		return $this->redirect(['index']);
	}
}